package com.xfs.controller.admin;

import com.alibaba.fastjson.JSON;
import com.xfs.entity.Employee;
import com.xfs.service.EmployeeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author dwj
 * @Date 2021-09-04 18:23
 */
@RestController
@RequestMapping("/admin/employee")
public class EmployeeController {
    @Resource
    private EmployeeService employeeService;

    /**
     * 员工登录
     *
     * @param username
     * @param password
     * @param session
     * @return
     */
    @RequestMapping("/login")
    public String login(String username, String password, HttpSession session) {
        Map<String, Object> map = new HashMap<String, Object>();
        // 调用员工登录的方法
        Employee employee = employeeService.login(username, password);
        if (employee != null) {
            // 保存当前登录用户
            session.setAttribute("loginUser", employee);
            map.put("success", true);   // 成功
        } else {
            map.put("success", false);  // 失败
            map.put("message", "账号或密码错误，登录失败");
        }
        return JSON.toJSONString(map);
    }
}
